
#include "lerp_tiling.h"
#include "register/op_def_registry.h"
#include "tiling/platform/platform_ascendc.h"


namespace optiling {
const uint32_t BLOCK_SIZE = 32;
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{

  LerpTilingData tiling;

    int32_t NUM = 8; //申请的内存块的个数（包括buffer和queue）
    auto ascendcPlatform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo());
    uint64_t ub_size; 
    ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ub_size);
    auto aivNum = ascendcPlatform.GetCoreNumAiv();

    auto dt = context->GetInputDesc(0)->GetDataType();
    uint32_t sizeofdatatype;
    if (dt == ge::DT_FLOAT16 || dt == ge::DT_BF16) 
    {
        sizeofdatatype = 2;
    }
    else 
    {
        sizeofdatatype = 4;
    }

    uint32_t ALIGN_NUM = BLOCK_SIZE / sizeofdatatype;

    // const gert::StorageShape* start_shape = context->GetInputShape(0);
    // const gert::StorageShape* end_shape = context->GetInputShape(0);
    // const gert::StorageShape* weight_shape = context->GetInputShape(0);

    // auto start_shape_dim = start_shape->GetStorageShape().GetDimNum();
    // auto end_shape_dim = end_shape->GetStorageShape().GetDimNum();
    // auto weight_shape_dim = weight_shape->GetStorageShape().GetDimNum();


    uint32_t start_length = context->GetInputTensor(0)->GetShapeSize();
    uint32_t end_length = context->GetInputTensor(1)->GetShapeSize();
    uint32_t weight_length = context->GetInputTensor(2)->GetShapeSize();

    // start end的max ,与min
    total_length = std::max<uint32_t>(start_length, end_length);
    min_length = std::min<uint32_t>(start_length, end_length);
    // 若weight是tensor 则三者的max, min
    if(weight_length != 1)
    {
        total_length = std::max<uint32_t>(total_length, weight_length);
        min_length = std::min<uint32_t>(min_length, weight_length);
    }

    auto dt = context->GetInputDesc(0)->GetDataType();
    uint32_t sizeofdatatype;
    if (dt == ge::DT_FLOAT16 || dt == ge::DT_BF16) 
    {
        sizeofdatatype = 2;
    }
    else 
    {
        sizeofdatatype = 4;
    }

    uint32_t ALIGN_NUM = BLOCK_SIZE / sizeofdatatype;
    uint32_t tiling_size = ((ub_size) / BLOCK_SIZE / 2) / NUM;
    tiling_size = tiling_size <= 8 ? tiling_size : tiling_size / 8 * 8;
    uint32_t block_size = tiling_size * ALIGN_NUM;
    
    context->SetTilingKey(1);
    if (total_length != min_length) {
        context->SetTilingKey(2);
        block_size = std::min(block_size, min_length);
        while (min_length % block_size || block_size % ALIGN_NUM) {
            block_size -= 1;
        }
    }

    aivNum = (aivNum < total_length / block_size) ? aivNum : (total_length / block_size);
    aivNum = aivNum >= 1 ? aivNum : 1;

    uint32_t core_size = (total_length / aivNum) / (ALIGN_NUM * 8) * (ALIGN_NUM * 8);
    uint32_t core_remain = total_length - aivNum * core_size;

    context->SetBlockDim(aivNum);

    tiling.set_ALIGN_NUM(ALIGN_NUM);
    tiling.set_core_size(core_size);
    tiling.set_core_remain(core_remain);
    tiling.set_block_size(block_size);

    tiling.set_total_length(total_length);
    tiling.set_start_length(start_length);
    tiling.set_end_length(end_length);
    tiling.set_weight_length(weight_length);

    printf("core_size: %u \n", core_size);
    printf("core_remain: %u \n", core_remain);
    printf("total_length: %u \n", total_length);
    printf("start_length: %u \n", start_length);
    printf("end_length: %u \n", end_length);
    printf("weight_length: %u \n", weight_length);
    printf("block_size: %u \n", block_size);



  tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
  context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());

  return ge::GRAPH_SUCCESS;
}
}


namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
}


namespace ops {
class Lerp : public OpDef {
public:
    explicit Lerp(const char* name) : OpDef(name)
    {
        this->Input("start")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("end")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("weight")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("y")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});

        this->SetInferShape(ge::InferShape);

        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend310b");

    }
};

OP_ADD(Lerp);
}


// uint32_t input_dim = start_shape_dim;
// if(input_dim < end_shape)
// {
//     input_dim = end_shape;
// }
// if(input_dim < weight_shape)
// {
//     input_dim = weight_shape;
// }

// int start[input_dim], end[input_dim], weight[input_dim], input[input_dim];
// for(int i = 0; i < input_dim; i++)
// {
//     start[i] = 0;
//     end[i] = 0;
//     weight[i] = 0;
//     input[i] = 0;
// }

// for (int i = 0; i < start_shape_dim; i++){
//     start[input_dim - start_shape_dim + i] = start_shape->GetStorageShape().GetDim(i);
// } 
// for (int i = 0; i < end_shape_dim; i++){
//     end[input_dim - end_shape_dim + i] = end_shape->GetStorageShape().GetDim(i);
// }
// for (int i = 0; i < weight_shape_dim; i++){
//     end[input_dim - weight_shape_dim + i] = weight_shape->GetStorageShape().GetDim(i);
// }

// for(int i = 0; i < input_dim; i++)
// {
//     input[i] = start[i];
//     if(input[i] < end[i])
//     {
//         input[i] = end[i];
//     }
//     if(input[i] < weight[i])
//     {
//         input[i] = weight[i];
//     }
// }   

// int ii;
// for(ii = 0; ii < input_dim; ii++)
// {
//     if(!((x0[ii] == input[ii]) && (x1[ii] == input[ii]) && (x2[ii] == input[ii])))
//         break;
// }

// if(ii == input_dim) //无广播
// {
//     uint32_t totalLength = context->GetInputTensor(0)->GetShapeSize();
//     uint32_t totalLengthAligned = ((totalLength + (ALIGN_NUM - 1)) / ALIGN_NUM) * ALIGN_NUM;
    
// }
// else    //广播
// {
//     for(int i = (input_dim -1); i <= 0; i--)
//     {//从低往高，依次检查，确定是否满足广播要求
//         if(!((x0[i]==input[i] || x0[i]==1 || x0[i]==0) && (x1[i]==input[i] || x1[i]==1 || x1[i]==0) && (x2[i]==input[i] || x2[i]==1 || x2[i]==0)))
//         {
//             return ge::GRAPH_FAILED;    
//         }
//     }
//     if(input_dim == 2)
//     {//二维广播
//         uint32_t x_D1, start_N0, end_N0, weight_N0;
//         tilingkey_ot = 2;
//         tile_num = input[0];
//         x_D1 = input[1];
//         tileLength = ((input[1] + ALIGN_NUM - 1)/ALIGN_NUM) * ALIGN_NUM;
//         if(start[0] == input[0])
//         {
//             start_N0 = 1;
//         }
//         else
//         {
//             start_N0 = input[0];
//         }
//         if(end[0] == input[0])
//         {
//             end_N0 = 1;
//         }
//         else
//         {
//             end_N0 = input[0];
//         }
//         if(weight[0] == input[0])
//         {
//             weight_N0 = 1;
//         }
//         else
//         {
//             weight_N0 = input[0];
//         }

//         blockLength =  (((input[0] * input[1]) + (ALIGN_NUM - 1))/ALIGN_NUM) * (ALIGN_NUM); 

//         tiling.set_blockLength(blockLength);
//         tiling.set_tileNum(tile_num);
//         tiling.set_tileLength(tileLength);
//         tiling.set_x_D1(x_D1);
//         tiling.set_start_N0(start_N0);
//         tiling.set_end_N0(end_N0);
//         tiling.set_weight_N0(weight_N0);

//         tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
//         context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
//         size_t *currentWorkspace = context->GetWorkspaceSizes(1);
//         currentWorkspace[0] = 0;
//         return ge::GRAPH_SUCCESS;    
//     }

// }